/**
 * 
 */
package com.example.ad_project;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.nus.iss.ad.model.DepartmentInventoryDetailsModel;
import com.nus.iss.ad.model.RetrivalDetailsModel;
import com.nus.iss.ad.model.RetrivalListModel;

/**
 * @author Sachin
 * 
 */
public class RetrivalDetailsAdapter extends ArrayAdapter<DepartmentInventoryDetailsModel> {

	private Context context;
	private DepartmentInventoryDetailsModel[] values;

	public RetrivalDetailsAdapter(Context context, DepartmentInventoryDetailsModel[] values) {
		super(context, R.layout.retrival_details_row_layout, values);
		this.context = context;
		this.values = values;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			v = inflater.inflate(R.layout.retrival_details_row_layout, null);
		}
		TextView deptName = (TextView) v.findViewById(R.id.deptName);
		final TextView requiredQty = (TextView) v.findViewById(R.id.requiredQtyTV);
		EditText expectedQty = (EditText) v.findViewById(R.id.expectedQtyET);
		final TextView errorText = (TextView) v.findViewById(R.id.errorTV);
		
		
		
		// CheckBox c1=(CheckBox)v.findViewById(R.id.checkBox1);
		// @SuppressWarnings("deprecation")
		// AbsoluteLayout layout = (AbsoluteLayout)
		// v.findViewById(R.layout.retrival_list_row_layout);

		final DepartmentInventoryDetailsModel didm = values[position];
		String deptNameString = didm.getDeptName();
		String retrivableQtyString = Integer.toString(didm.getQtyRetrived());
		String expectedQtyString = Integer.toString(didm.getQtyExpected());
		deptName.setText(deptNameString);
		requiredQty.setText(retrivableQtyString);
		expectedQty.setText(expectedQtyString);
		
		final String val = retrivableQtyString;

		
		 expectedQty.addTextChangedListener(new TextWatcher() { 
			 String value = val; 
             public void afterTextChanged(Editable s) {  } 
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {  } 
             
             public boolean isValidNumber(Class c, String numString) {
            	  try {
            	    if (c == double.class || c == Double.class) {
            	      Double.parseDouble(numString);
            	    } else if (c == int.class || c == Integer.class) {
            	      Integer.parseInt(numString);
            	    } else if (c == float.class || c == Float.class) {
            	      Float.parseFloat(numString);
            	    } else if (c == long.class || c == Long.class) {
            	      Long.parseLong(numString);
            	    }
            	  } catch (Exception ex) {
            	    return false;
            	  }
            	  return true;
            	}
             
             
             public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TEST", s+"  In onTextChanged");
             Log.d("TEST", requiredQty.getText().toString()+"  In onTextChanged");
             
             //if(requiredQty.getText() != null && requiredQty.getText().toString() != ""){
             if(s !="" && isValidNumber(Integer.class, s.toString())){
             int retrived = Integer.parseInt(value);
             
             int expected = Integer.parseInt(s.toString());
             if(retrived < expected){
            	 errorText.setText("please enter proper value");
            	 //didm.setCheckFlag(true);
             }else {
            	 errorText.setText("");
			}
             
            }else {
            	 errorText.setText("please enter proper value");
			}
             
            if(errorText.getText().toString().equalsIgnoreCase("please enter proper value")){
    			 didm.setCheckFlag(true);
    		}else if(errorText.getText().toString().equalsIgnoreCase("")) {
    			didm.setQtyExpected(Integer.parseInt(s.toString()));
    			didm.setCheckFlag(false);
    		}
             
             }
         }); 
		 
		
		 
		
		// c1.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)context);

		return v;

	}

}
